
package data;

import java.io.IOException;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.ObjectInputStream;


/**
 *
 * @author ruben.bordallo
 */
public final class ObjectController<T> {

    public ObjectController() {}

    /**
    *  Guarda un objecte que implementi serialitzable al disc
    *
    * @author ruben.bordallo
    * @param obj es l'objecte que es guardara en disc
    * @param path es la direcció on es guarda l'objecte obj
    * @return -
    * @throws IOException
    */
    public void saveObject(T obj, String path) throws IOException {
        ObjectOutputStream oOut = null;
            oOut = new ObjectOutputStream(new FileOutputStream(path));
            oOut.writeObject(obj);
            oOut.flush();
            oOut.close();
    }
    
    /**
    *  Carga un objecte que implementi serialitzable de disc a memoria
    *
    * @author ruben.bordallo
    * @param path es la direcció on s'ha guardat l'objecte
    * @return objecte de tipus T
    * @throws IOException
    * @throws ClassNotFoundException
    * @throws FileNotFoundException
    */
    public  T loadObject(String path) throws IOException,ClassNotFoundException,FileNotFoundException {
            ObjectInputStream oIn = null;
            oIn = new ObjectInputStream(new FileInputStream(path));
            T uber = (T)oIn.readObject();
            oIn.close();
            return uber;
    }
}